
package com.epam.android.training.task;

import com.epam.android.training.task.test.Question;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author pavel_busko
 * 
 * This activity presents test (question -> answer flow) and returns percent of
 * right answers.
 */
public class RunTestActivity extends Activity {

    private static final int PERCENT_MULTIPLIER = 100;

    private TextView questionText;

    private Button buttonAnswer;

    private Question currentQuestion;

    private ArrayAdapter<CharSequence> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.run_test);
        ((TextView) findViewById(R.id.text_test_name)).setText(getIntent().getStringExtra(
                TestListActivity.TEST_NAME_KEY));
        questionText = (TextView) findViewById(R.id.text_question);

        buttonAnswer = (Button) getLayoutInflater().inflate(R.layout.button_answer, null);

        final ArrayList<Question> questions = getIntent().getParcelableArrayListExtra(
                TestListActivity.TEST_KEY);

        final Iterator<Question> iterator = questions.iterator();
        currentQuestion = iterator.next();

        adapter = new ArrayAdapter<CharSequence>(getApplicationContext(),
                android.R.layout.simple_list_item_single_choice);
        final ListView listView = (ListView) findViewById(R.id.answer_list);

        listView.addFooterView(buttonAnswer, null, false);
        listView.setAdapter(adapter);

        setView(!iterator.hasNext());

        buttonAnswer.setOnClickListener(new OnClickListener() {
            int correctCount = 0;

            @Override
            public void onClick(View v) {
                int checkedPosition = listView.getCheckedItemPosition();
                if (checkedPosition == currentQuestion.getCorrect()) {
                    correctCount++;
                }
                listView.setItemChecked(0, false);
                if (iterator.hasNext()) {
                    currentQuestion = iterator.next();
                    setView(!iterator.hasNext());
                } else {
                    Intent intent = new Intent();
                    intent.putExtra(TestListActivity.PERCENT_KEY, correctCount * PERCENT_MULTIPLIER
                            / questions.size());
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

    private void setView(boolean isLast) {
        adapter.clear();
        for (String b : currentQuestion.getAnswers()) {
            adapter.add(b);
        }
        questionText.setText(currentQuestion.getText());
        if (isLast) {
            buttonAnswer.setText(R.string.button_finish_title);
        }
    }
}
